<?php
require_once '../../server/utils/functions.php';
require_once '../../server/model/User.php';
require_once '../../server/model/Crypt.php';
$db = db();
$crypt = new Crypt();
if(isset($_POST["USR_MAIL"])&& !empty($_POST["USR_MAIL"]) && preg_match("#^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$#", $_POST["USR_MAIL"])){
    $parameters["USR_MAIL"] = utf8_decode(htmlentities($_POST["USR_MAIL"]));
}else{
    echo("Caca1");
    exit(0);
}  
if(isset($_POST["USR_PASSWORD"])&& !empty($_POST["USR_PASSWORD"])
        && isset($_POST["USR_PWD_CONF"])&& $_POST["USR_PASSWORD"]==$_POST["USR_PWD_CONF"]){
    $parameters["USR_PASSWORD"] = $crypt->crypt(utf8_decode(htmlentities($_POST["USR_PASSWORD"])));
}else{
    echo("Caca2");
    exit(0);
}
$req = $db->prepare("INSERT INTO USER(USR_MAIL,USR_PASSWORD) VALUES (:USR_MAIL, :USR_PASSWORD)");
if($req->execute($parameters)){
//$datas = $req->fetch()){
    echo "Ok";
    $user = new User($parameters["USR_MAIL"],$parameters["USR_PASSWORD"]);
    $_SESSION["USER"]= $user;
}else{
    echo "Caca3";
}
$req->closeCursor();
?>
